/**
 *
 *  Copyright (c) 2011, AstroDAbis
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *      * Redistributions of source code must retain the above copyright notice,
 *        this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright notice,
 *        this list of conditions and the following disclaimer in the documentation
 *        and/or other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package org.jiscinvolve.astrodabis.barberry.control.jobs;

import java.net.URL;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.NotBlank;

import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

import org.jiscinvolve.astrodabis.barberry.common.core.Job;
import org.jiscinvolve.astrodabis.barberry.common.core.Job.AnnotType;
import org.jiscinvolve.astrodabis.barberry.common.core.Job.TagNameSrc;
import org.jiscinvolve.astrodabis.barberry.common.core.Job.TagDataSrc;
import org.jiscinvolve.astrodabis.barberry.common.core.Job.SourceType;

import org.jiscinvolve.astrodabis.barberry.common.core.Womble;

import org.jiscinvolve.astrodabis.barberry.common.data.TapTable;
import org.jiscinvolve.astrodabis.barberry.common.data.TapColumn;
import org.jiscinvolve.astrodabis.barberry.common.data.TapService;

/**
 *
 */
public class JobBean
extends Job.Bean
    {

    public JobBean()
        {
        super();
        }

    public JobBean(Job job)
        {
        super(job);

        if (job.columnOne() != null)
            {
            this.cidOne = job.columnOne().ident().toString();

            this.tabOne = job.columnOne().parent();
            this.tidOne = job.columnOne().parent().ident().toString();

            this.srvOne = job.columnOne().parent().parent();
            this.sidOne = job.columnOne().parent().parent().ident().toString();

            }

        if (job.columnTwo() != null)
            {
            this.cidTwo = job.columnTwo().ident().toString();

            this.tabTwo = job.columnTwo().parent();
            this.tidTwo = job.columnTwo().parent().ident().toString();

            this.srvTwo = job.columnTwo().parent().parent();
            this.sidTwo = job.columnTwo().parent().parent().ident().toString();
            }

        }

    private String sidOne ;
    public String getSidOne()
        {
        return this.sidOne ;
        }
    public void setSidOne(String value)
        {
        this.sidOne = value ;
        }

    private String tidOne ;
    public String getTidOne()
        {
        return this.tidOne ;
        }
    public void setTidOne(String value)
        {
        this.tidOne = value ;
        }

    private String cidOne ;
    public String getCidOne()
        {
        return this.cidOne ;
        }
    public void setCidOne(String value)
        {
        this.cidOne = value ;
        }

    public Boolean getJoined()
        {
        return (this.annotType == AnnotType.JOINED) ;
        }
    public void setJoined(Boolean value)
        {
        if (value)
            {
            this.annotType = AnnotType.JOINED ;
            }
        else {
            this.annotType = AnnotType.SINGLE ;
            }
        }

    private String sidTwo ;
    public String getSidTwo()
        {
        return this.sidTwo ;
        }
    public void setSidTwo(String value)
        {
        this.sidTwo = value ;
        }

    private String tidTwo ;
    public String getTidTwo()
        {
        return this.tidTwo ;
        }
    public void setTidTwo(String value)
        {
        this.tidTwo = value ;
        }

    private String cidTwo ;
    public String getCidTwo()
        {
        return this.cidTwo ;
        }
    public void setCidTwo(String value)
        {
        this.cidTwo = value ;
        }
 
    private CommonsMultipartFile sourceFile;
    public CommonsMultipartFile getSourceFile()
        {
        return sourceFile;
        }
    public void setSourceFile(CommonsMultipartFile file)
        {
        this.sourceFile = file;
        }

    private String sourceQuerySid ;
    public String getSourceQuerySid()
        {
        return sourceQuerySid ;
        }
    public void setSourceQuerySid(String value)
        {
        this.sourceQuerySid = value ;
        }

    private String sourceQueryText ;
    public String getSourceQueryText()
        {
        return sourceQueryText ;
        }
    public void setSourceQueryText(String value)
        {
        this.sourceQueryText = value ;
        }

    private TapService srvOne = null ;
    public TapService srvOne()
        {
        return this.srvOne ;
        }

    private TapService srvTwo = null ;
    public TapService srvTwo()
        {
        return this.srvTwo ;
        }

    private TapTable tabOne = null ;
    public TapTable tabOne()
        {
        return this.tabOne ;
        }

    private TapTable tabTwo = null ;
    public TapTable tabTwo()
        {
        return this.tabTwo ;
        }

    public void resolve(Womble womble)
        {
        if (sidOne != null)
            {
            srvOne = womble.services().select(
                womble.services().ident(
                    sidOne
                    )
                );
            if (srvOne != null)
                {
                if (tidOne != null)
                    {
                    tabOne = srvOne.tables().select(
                        srvOne.tables().ident(
                            tidOne
                            )
                        );
                    if (tabOne != null)
                        {
                        if (cidOne != null)
                            {
                            columnOne = tabOne.columns().select(
                                tabOne.columns().ident(
                                    cidOne
                                    )
                                );
                            }
                        }
                    }
                }
            }

        if (sidTwo != null)
            {
            srvTwo = womble.services().select(
                womble.services().ident(
                    sidTwo
                    )
                );
            if (srvTwo != null)
                {
                if (tidTwo != null)
                    {
                    tabTwo = srvTwo.tables().select(
                        srvTwo.tables().ident(
                            tidTwo
                            )
                        );
                    if (tabTwo != null)
                        {
                        if (cidTwo != null)
                            {
                            columnTwo = tabTwo.columns().select(
                                tabTwo.columns().ident(
                                    cidTwo
                                    )
                                );
                            }
                        }
                    }
                }
            }
        }

    }

